﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;


namespace vantai
{
    public partial class NhanVienCapNhatHuyDangKy : Form
    {
        public NhanVienCapNhatHuyDangKy()
        {
            InitializeComponent();
        }
        string manv = Login.MaNV;
        string sql = 
"select ld.MaNV, nv.TenNV, ld.MaTuyen, t.TenTuyen, ld.MaXe,ld.NgayDi from NhanVien nv, Tuyen t, DangKyLichDi ld ";

        bool ischangeddatetime = false;// kiem tra datatimepicker thay doi
        private void LoadDanhSachLichDiDaThamGia(string sql)
        {
            DangKyLichDiBUS dkldbus = new DangKyLichDiBUS();
            DataTable dt = dkldbus.DanhSachLichDi(sql);
            int i = 1;
            dtgvDSLichDaDangKy.Rows.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                string ngd = DateTime.Parse(dr[5].ToString()).ToShortDateString();
                dtgvDSLichDaDangKy.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), ngd);
                i++;
            }

            dtgvDSLichDaDangKy.Columns[0].Width = 50;
            dtgvDSLichDaDangKy.Columns[1].Width = 80;
            dtgvDSLichDaDangKy.Columns[2].Width = 150;
            dtgvDSLichDaDangKy.Columns[3].Width = 90;
            dtgvDSLichDaDangKy.Columns[4].Width = 150;
            dtgvDSLichDaDangKy.Columns[5].Width = 80;
            dtgvDSLichDaDangKy.Columns[6].Width = 120;
        }

        private void NhanVienCapNhatHuyDangKy_Load(object sender, EventArgs e)
        {
            // tuyen
            TuyenBUS tb = new TuyenBUS();
            List<TuyenDTO> lsttuyen = tb.DanhSachTuyen();
            lsttuyen.Insert(0, new TuyenDTO(-1, "Chọn tuyến cần xem"));
            cbxDanhSachTuyen.DataSource = lsttuyen;
            cbxDanhSachTuyen.DisplayMember = "TenTuyen";
            cbxDanhSachTuyen.ValueMember = "MaTuyen";

            // xe

            XeBUS xb = new XeBUS();
            List<XeDTO> lstxe = xb.DanhSachXe();
            lstxe.Insert(0, new XeDTO(-1,"Chọn xe cần xem"));
            cbxDanhSachXe.DataSource = lstxe;
            cbxDanhSachXe.DisplayMember = "maxestring";
            cbxDanhSachXe.ValueMember = "maXe";


            sql += "where nv.MaNV=ld.MaNV and t.MaTuyen=ld.MaTuyen and nv.MaNV='"+manv+"'";


            //DangKyLichDiBUS dkldbus = new DangKyLichDiBUS();
            //DataTable dt = dkldbus.DanhSachLichDi(sql);
            //int i = 1;
            //foreach (DataRow dr in dt.Rows)
            //{
            //    string ngd = DateTime.Parse(dr[5].ToString()).ToShortDateString();
            //    dtgvDSLichDaDangKy.Rows.Add(i, dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), ngd);
            //    i++;
            //}

            //dtgvDSLichDaDangKy.Columns[0].Width = 50;
            //dtgvDSLichDaDangKy.Columns[1].Width = 80;
            //dtgvDSLichDaDangKy.Columns[2].Width = 150;
            //dtgvDSLichDaDangKy.Columns[3].Width = 90;
            //dtgvDSLichDaDangKy.Columns[4].Width = 150;
            //dtgvDSLichDaDangKy.Columns[5].Width = 80;
            //dtgvDSLichDaDangKy.Columns[6].Width = 120;



            LoadDanhSachLichDiDaThamGia(sql);



        }

        private void cbxDanhSachXe_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (cbxDanhSachXe.SelectedIndex != 0)
            {
                string sql2 = sql;
                int mxe = Int32.Parse(cbxDanhSachXe.SelectedValue.ToString());
                sql2 += " and ld.MaXe=" + mxe;
                if (cbxDanhSachTuyen.SelectedIndex != 0)
                {
                    int mtuyen = Int32.Parse(cbxDanhSachTuyen.SelectedValue.ToString());
                    sql2 += " and t.MaTuyen=" + mtuyen;
                }
                if (ischangeddatetime)
                {
                    sql2 += " and ld.NgayDi='" + dtpNgayDi.Value.ToShortDateString() + "'";
                }
                LoadDanhSachLichDiDaThamGia(sql2);

            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        int rowindex = -1;
        private void dtgvDSLichDaDangKy_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            rowindex = e.RowIndex;
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void hủyLịchNàyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (rowindex != -1)
            {
                if (MessageBox.Show("Bạn có thật sự muốn hủy đăng ký vào lịch này không?", "Thông báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    DangKyLichDiBUS ldbus=new DangKyLichDiBUS();
                    DangKyLichDiDTO ld = new DangKyLichDiDTO();
                    ld.MaNV = dtgvDSLichDaDangKy.Rows[rowindex].Cells[1].Value.ToString();
                    ld.MaTuyen =int.Parse( dtgvDSLichDaDangKy.Rows[rowindex].Cells[3].Value.ToString());
                    ld.MaXe=int.Parse(dtgvDSLichDaDangKy.Rows[rowindex].Cells[5].Value.ToString());
                    ld.NgayDi = DateTime.Parse(dtgvDSLichDaDangKy.Rows[rowindex].Cells[6].Value.ToString());

                    if (ldbus.HuyDangKyLichDi(ld))
                    {
                        ldbus.HuyslDangKyLichDi(ld);
                        MessageBox.Show("Hủy tham gia thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadDanhSachLichDiDaThamGia(sql);
                    }
                }
            }
            else
            {
                MessageBox.Show("Vui lòng chọn một lịch đi trước", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnXemHet_Click(object sender, EventArgs e)
        {
            LoadDanhSachLichDiDaThamGia(sql);
        }

        
        private void cbxDanhSachTuyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxDanhSachTuyen.SelectedIndex != 0) // co chon xem theo tuyen
            {
               string sql2 = sql;
                int mtuyen=Int32.Parse(cbxDanhSachTuyen.SelectedValue.ToString());
                sql2 += " and t.MaTuyen=" + mtuyen;
                if (cbxDanhSachXe.SelectedIndex != 0)// co chon xem theo xe
                {
                    int mxe = Int32.Parse(cbxDanhSachXe.SelectedValue.ToString());
                    sql2 += " and ld.MaXe=" + mxe;
                }
                if (ischangeddatetime)
                {
                    sql2+=" and ld.NgayDi='"+ dtpNgayDi.Value.ToShortDateString()+"'";
                }
                LoadDanhSachLichDiDaThamGia(sql2);
            }
        }

        private void dtpNgayDi_ValueChanged(object sender, EventArgs e)
        {
            ischangeddatetime = true;
            string sql2=sql;
            sql2+=" and ld.NgayDi='"+dtpNgayDi.Value.ToShortDateString()+"'";
            if (cbxDanhSachTuyen.SelectedIndex != 0)
            {

                int mtuyen = Int32.Parse(cbxDanhSachTuyen.SelectedValue.ToString());
                sql2 += " and t.MaTuyen=" + mtuyen;

            }
            if (cbxDanhSachXe.SelectedIndex != 0)
            {
                int mxe = Int32.Parse(cbxDanhSachXe.SelectedValue.ToString());
                sql2 += " and ld.MaXe=" + mxe;
            }

            LoadDanhSachLichDiDaThamGia(sql2);

            //MessageBox.Show("heyhey");
        }

    }
}
